//
//  BannerViewController.m
//  Article
//
//  Created by LONG on 16/6/12.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "BannerViewController.h"
#import "OllTableViewCell.h"

@interface BannerViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *dataarr;

@end

@implementation BannerViewController

- (UITableView *)myTableView{
    if (nil == _myTableView){
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49)];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [_myTableView registerNib:[UINib nibWithNibName:@"OllTableViewCell" bundle:nil] forCellReuseIdentifier:@"OllTableViewCell"];
        _myTableView.showsVerticalScrollIndicator = NO;
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [_myTableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(downMJ)];
        
        [self setExtraCellLineHidden:_myTableView];
        
        
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"素材库";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    
    [self myTableView];
    [self requestTableview];

}

//下拉刷新
-(void)downMJ{
    [self requestTableview];
    
    [_myTableView.header endRefreshing];
}

- (void)requestTableview{
    _dataarr = [NSMutableArray arrayWithCapacity:0];;
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:BANNERLIST_URL parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            //建立模型
            for (NSDictionary*dic in [responseObject objectForKey:@"list"]) {
                MateModel*model=[[MateModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                //追加数据
                [self.dataarr addObject:model];
            }
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        
        [self.myTableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}

#pragma mark tableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OllTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataarr.count == 0) {
        return cell;
    }
    MateModel*model=self.dataarr[indexPath.row];
    [cell.ad_img sd_setImageWithURL:[NSURL URLWithString:model.demo] placeholderImage:[UIImage imageNamed:@"tonglan"]];
    cell.imagebut.hidden = YES;
    cell.backgroundColor = UIColorFromRGB(0xefeff1);

    return cell;
}

#pragma mark delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*WIDTH/320;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MateModel*model=self.dataarr[indexPath.row];

    if (_block) {
        
        _block(model.id,model.demo);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
